#pragma once


//------------------------------------------------------------------------
//
//  Name:			CGETimer.h
//
//  Desc:			A class deal with Time
//
//  Author:			Crane Shen
//
//  CGE:			Crane's Game Engine
//
//  Create Time:	4/30 2010
//
//------------------------------------------------------------------------


#include "SDL.h"

const int DOUBLE_CLICK_MAX_DELAY = 300;
const int CLICK_MAX_DELAY = 50;

//The frames per second,lock
const int g_FramePerSecond = 50;
//Whether or not to cap the frame rate
static bool	  g_LockFrame = true;

//Keep track of the current frame,per second
static int	  g_Frame = 0;

class CGETimer
{
public:
	CGETimer(void);
	virtual ~CGETimer(void);

	void			Start();
	void			Stop();

	void			Pause();
	void			Unpause();

	int				GetTicks();

	bool			IsStarted(){return m_Started;}
	bool			IsPaused(){return m_Paused;}

	void			Delay(unsigned int delay){SDL_Delay(delay);}

private:
	//The timer starting time
	unsigned int	m_StartTime;
	unsigned int	m_PauseTime;

	//The timer start/stop flag
	bool			m_Started;
	bool			m_Paused;
};

//this function should call in every frame and Timer need restart at the frame start.
static void  LockFrame(CGETimer* timer)
{
	if(g_LockFrame == true)
	{
		if(timer->GetTicks() < 1000 / g_FramePerSecond )
		{
			//Sleep the remaining frame time
			timer->Delay( ( 1000 / g_FramePerSecond ) - timer->GetTicks() );
		}
	}
}

static bool DoubleClickDetected (void) 
{ 
	static Uint32 LastClickTicks; 
	Uint32 CurrentClickTicks; 
	/* The first time this function is called, LastClickTicks has not been initialised yet. */ 
	if (LastClickTicks == 0) 
	{ 
		LastClickTicks = SDL_GetTicks (); 
		return false; 
	} 
	else 
	{ 
		CurrentClickTicks = SDL_GetTicks (); 
		/* If the period between the two clicks is smaller or equal to a pre-defined number, we report a DoubleClick event. */ 
		if (CurrentClickTicks - LastClickTicks <= DOUBLE_CLICK_MAX_DELAY) 
		{ 
			/* Update LastClickTicks and signal a DoubleClick. */ 
			LastClickTicks = CurrentClickTicks; 
			return true; 
		} 
		else 
		{ 
			/* Update LastClickTicks and signal a SingleClick. */ 
			LastClickTicks = CurrentClickTicks; 
			return false; 
		} 
	} 
}
static bool ClickDetected (void) 
{ 
	static Uint32 LastClickTicks; 
	Uint32 CurrentClickTicks; 
	/* The first time this function is called, LastClickTicks has not been initialised yet. */ 
	if (LastClickTicks == 0) 
	{ 
		LastClickTicks = SDL_GetTicks (); 
		return false; 
	} 
	else 
	{ 
		CurrentClickTicks = SDL_GetTicks (); 
		/* If the period the clicks is smaller or equal to a pre-defined number, we report a not Click event. */ 
		if (CurrentClickTicks - LastClickTicks <= CLICK_MAX_DELAY) 
		{ 
			/* Update LastClickTicks and signal a notClick. */ 
			LastClickTicks = CurrentClickTicks; 
			return false; 
		} 
		else 
		{ 
			/* Update LastClickTicks and signal a Click. */ 
			LastClickTicks = CurrentClickTicks; 
			return true; 
		} 
	} 
}


//use this function by this example
//g_Frame++;
//if( gtimer.get_ticks() > 1000 )
//{
//	GetFPS(g_Frame,gtimer);
//	gtimer.Start();
//  g_Frame = 0;
//}
static int  GetFPS(int frame,CGETimer* gtimer)
{
	return  frame /( gtimer->GetTicks() / 1000.f ); 
}
